第 361 场力扣周赛

统计对称整数的数目

模拟。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public int countSymmetricIntegers(int low, int high) {
int ans = 0;
for (int i = low; i <= high; i++) {
int x = i, n = 0;
int[] aux = new int[10];
for (; x != 0; x /= 10) {
aux[n++] = x % 10;
}
if (n % 2 == 0) {
int sum = 0;
for (int j = 0; j < n / 2; j++) {
sum += aux[j] - aux[j + n / 2];
}
if (sum == 0) {
ans++;
}
}
}
return ans;
}
}

生成特殊数字的最少操作

比较简洁的暴力写法,当然从个位开始找 \(25,75,50,00,0\) 更快。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int minimumOperations(String num) {
int n = num.length(), ans = n;
for (int i = 0; i < n; i++) {
if (num.charAt(i) == '0') {
ans = Math.min(ans, n - 1);
}
for (int j = i + 1; j < n; j++) {
int x = (num.charAt(i) - '0') * 10 + num.charAt(j) - '0';
if (x % 25 == 0) {
ans = Math.min(ans, n - i - 2);
}
}
}
return ans;
}
}

统计趣味子数组的数目

最开始的思路是,找到所有满足 \(nums[i]\bmod modulo=k\) 的下标放入新的列表,然后在新列表中枚举左端点 \(i\),此时满足条件的右端点就是 \(i+k-1+j\times modulo\)。暴力解决的时间复杂度 \(O(n^{2})\),所以可以倒序枚举左端点,顺便记录间隔为 \(modulo\) 的后缀和。但是,这样解决还需要特判 \(k=0\) 的情况,总之很麻烦。

更好的做法是利用同余的性质。将所有 \(nums[i]\bmod modulo=k\) 的数字看作 \(1\),其他数字看作 \(0\),这样我们要求的就是满足 \((sum[r+1]-sum[l])\bmod modulo=k\) 的所有子数组的数目。我们可以枚举右端点,找到满足 \((sum[r+1]-k)\equiv sum[l]\pmod{modulo}\) 的左端点的个数,使用前缀和 + 哈希表即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public long countInterestingSubarrays(List<Integer> nums, int modulo, int k) {
long ans = 0L;
int n = nums.size(), sum = 0;
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
for (int x : nums) {
if (x % modulo == k) {
sum = (sum + 1) % modulo;
}
ans += map.getOrDefault((sum - k + modulo) % modulo, 0);
map.merge(sum, 1, Integer::sum);
}
return ans;
}
}

边权重均等查询

树上倍增求最近公共祖先,同时维护边权的计数。详细见灵神题解。(发现汪佬的写法更简单,在 DFS 的同时进行倍增,以及通过拷贝数组来维护边权的计数信息。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Solution {
private static final int M = 14;

public int[] minOperationsQueries(int n, int[][] edges, int[][] queries) {
List<int[]>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (int[] e : edges) {
int u = e[0], v = e[1], w = e[2] - 1;
g[u].add(new int[]{v, w});
g[v].add(new int[]{u, w});
}
int[] depth = new int[n];
int[][] cnt = new int[n][26];
int[][] parent = new int[M][n];
dfs(0, -1, g, depth, parent, cnt);
// 查询
int k = queries.length;
int[] ans = new int[k];
while (k-- != 0) {
int x = queries[k][0], y = queries[k][1];
int z = lca(x, y, depth, parent), max = 0;
for (int i = 0; i < 26; i++) {
max = Math.max(max, cnt[x][i] + cnt[y][i] - 2 * cnt[z][i]);
}
ans[k] = depth[x] + depth[y] - 2 * depth[z] - max;
}
return ans;
}

// DFS 的同时进行倍增,以及维护边权的计数
private void dfs(int x, int fa, List<int[]>[] g, int[] depth, int[][] parent, int[][] cnt) {
for (int i = 1; 1 << i <= depth[x]; i++) {
parent[i][x] = parent[i - 1][parent[i - 1][x]];
}
for (int[] t : g[x]) {
int y = t[0], w = t[1];
if (y != fa) {
parent[0][y] = x;
System.arraycopy(cnt[x], 0, cnt[y], 0, 26);
cnt[y][w]++;
depth[y] = depth[x] + 1;
dfs(y, x, g, depth, parent, cnt);
}
}
}

// 求最近公共祖先
private int lca(int x, int y, int[] depth, int[][] parent) {
if (depth[x] > depth[y]) {
int t = x;
x = y;
y = t;
}
// 先向上跳到相同深度
int step = depth[y] - depth[x];
for (int i = 0; i < 32; i++) {
if ((step >> i & 1) != 0) {
y = parent[i][y];
}
}
// 尽量向上跳
if (x != y) {
for (int i = M - 1; i >= 0; i--) {
int px = parent[i][x], py = parent[i][y];
if (px != py) {
x = px;
y = py;
}
}
x = parent[0][x];
}
return x;
}
}
作者

Ligh0x74

发布于

2023-09-04

更新于

2023-09-04

许可协议

评论